﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;

namespace NetCoreCMS.Web.Areas.WebApi.Controllers
{
    public class User_Transer_ListController : UserController
    {
        public ITransferService _transfer { get; set; }
        [Authorize]
        [HttpGet]
        public IActionResult Get(int page=1,int page_size=20,int from_to=0)//0转出，1转入
        {
            //检查用户是否登录
            Users userModel = GetUserInfo();
            if (userModel == null)
            {
                return Ok(new { status = 0, msg = "用户尚未登录或已超时！" });
            }
            page = page > 0 ? page : 1;
            string strwhere = "where from_user_id=" + userModel.id;
            if(from_to>0)
                strwhere = "where to_user_id=" + userModel.id;
            var list = _transfer.GetList(page, page_size, strwhere, "add_time desc,id desc");
            int totalcount = _transfer.RecordCount(strwhere);
            int pageCount = totalcount / page_size;
            if (pageCount < 1)
            {
                pageCount = 1;
            }
            if (totalcount % page_size > 0)
            {
                pageCount += 1;
            }
            if (pageCount <= 1)
            {
                pageCount = 1;
            }
            return Ok(new { status = 1, list = list, page = page, page_count = pageCount, total_count = totalcount });
        }
    }
}